package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zbkj.common.model.system.RandomRecord;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.service.dao.RandomRecordMapper;
import com.zbkj.service.service.RandomRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 随机记录表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-10-02
 */
@Service
public class RandomRecordServiceImpl extends ServiceImpl<RandomRecordMapper, RandomRecord> implements RandomRecordService {

    @Resource
    private RandomRecordMapper randomRecordMapper;

    @Override
    public int codeExistsCheck(int type, String randomCode) {
        if(StringUtils.isBlank(randomCode)) {
            return 1;
        }
        LambdaQueryWrapper<RandomRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RandomRecord::getType, type)
                .eq(RandomRecord::getRcode,randomCode)
                .last(" limit 1");
        int count = this.count(wrapper);
        return count>0 ? 1 : 0;
    }

    @Override
    public void saveRecord(RandomRecord randomRecord) {
        if(Objects.isNull(randomRecord)) {
            return;
        }
        this.save(randomRecord);
    }

    @Override
    public void delRecode(int type, Long sourceId) {
        if(Objects.isNull(sourceId)) {
            return;
        }
        LambdaQueryWrapper<RandomRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RandomRecord::getType,type)
                .eq(RandomRecord::getSourceId,sourceId);
        randomRecordMapper.delete(wrapper);
    }

}
